package com.xca.is.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.xca.is.R;
import com.xca.is.entity.Wallpaper;
import com.xca.is.utils.ImageDownloader;

import java.util.List;

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.ViewHolder>{
    private List<Wallpaper.DataDTO> list ;
    private Context context ;
    private Activity activity ;
    public WallpaperAdapter(List<Wallpaper.DataDTO> list , Context context , Activity activity)
    {
        this.list = list ;
        this.context = context ;
        this.activity = activity ;
    }

    public void updateData(List<Wallpaper.DataDTO> list)
    {
        this.list = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context , R.layout.image_item , null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Wallpaper.DataDTO data = list.get(position) ;
        ImageDownloader imageDownloader = new ImageDownloader(context , activity);

        Picasso.get()
                .load(data.getSrc())
                .into(holder.imageView);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                new AlertDialog.Builder(context)
                        .setMessage("是否下载此壁纸")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                imageDownloader.saveImage(data.getSrc());
                            }
                        })
                        .create()
                        .show();
                return false; // 返回false, 表示事件没有被消费，会继续传递给父控件，
                // 比如RecyclerView，会继续传递给RecyclerView的onTouchEvent方法，然后由RecyclerView自己处理。
            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_item_img);
        }
    }
}
